package cn.livingCloud.controller.user;


import cn.livingCloud.dto.AccountChangePasswordDto;
import cn.livingCloud.dto.ChangePasswordDto;
import cn.livingCloud.dto.UserAccountDto;
import cn.livingCloud.dto.UserAccountListDto;
import cn.livingCloud.service.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;

/**
 * Created by DingJie  on 2015/12/4.
 */
@Controller
@RequestMapping("/admin/user_account")
public class UserAccountController {

    @Qualifier("userAccountServiceImpl")
    @Autowired
    private UserAccountService userAccountService;

    //显示用户账户列表
    @RequestMapping("/list.htm")
    public String list(UserAccountListDto userAccountListDto, Model model) {
        userAccountService.getUserAccountList(userAccountListDto);
        model.addAttribute("listDto", userAccountListDto);
        return "user/user_account_list";
    }

    //更改状态栏
    @RequestMapping("/change_state")
    public String changeState(String uuid, boolean state, Model model) {
        UserAccountDto userAccountDto = userAccountService.updateState(uuid, state);
        model.addAttribute("accountDto", userAccountDto);
        return "user/user_account_list_item";
    }

    //进入更改密码界面
    @RequestMapping("/change_password_from.htm")
    public String loadChangePassword(String uuid, Model model) {
        AccountChangePasswordDto userAccountDto = userAccountService.findUserAccountDtoByUuid(uuid);
        model.addAttribute("accountDto", userAccountDto);
        return "user/change_password_from";
    }

    //更改密码
    @RequestMapping(value = "/change_password.htm", method = RequestMethod.POST)
    public String changePassword(@ModelAttribute("accountDto") @Valid AccountChangePasswordDto accountDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "user/change_password_from";
        }
        userAccountService.updatePassword(accountDto);
        model.addAttribute("change_password", "success");
        return "redirect:list.htm";
    }

    //修改自己的密码
    @RequestMapping("/change_self_password.htm")
    public String changeSelfPassword(Model model) {
        model.addAttribute("formDto", new ChangePasswordDto());
        return "user/change_self_password";
    }

    //提交修改自己的密码
    @RequestMapping(value = "/change_self_password.htm", method = RequestMethod.POST)
    public String submitChangePassword(@ModelAttribute("formDto") @Valid ChangePasswordDto dto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "user/change_self_password";
        }
        userAccountService.changeSelfPwd(dto);
        model.addAttribute("change", "success");
        return "redirect:/admin/index.htm";
    }
}
